/******************************辅助类:ContourLine******************************/
import * as Cesium from "cesium";

/**
 * ContourLine等高线类
 * @param { number } contourValue 等高线值
 * @param { Cesium.Cartesian3[][] } positionsList n条等高线对应的n个顶点列表
 */
function ContourLine(contourValue, positionsList) {
    this.contouerValue = contourValue;
    this.positionsList = positionsList;
}

/**
 * 等高线默认配置项
 */
ContourLine.defaulltOptions = {
    color: Cesium.Color.RED,
    width: 1.5,
};

const defaultOptions = Object.freeze({
    color: Cesium.Color.RED,
    spacing: 120.0,
    width: 1.5,
    granularity: Math.PI / Math.pow(2, 11) / 64.0, /// 64.0,
    showTINs: false,
});

/**
 * 等高线默认的材质Appearance
 */
ContourLine.defaultAppearance = new Cesium.PolylineColorAppearance({
    translucent: false,
});

/**
 * 创建等高线图元集合-[贴地线]
 * @param { Cesium.Appearance } appearance polyline线的材质
 * @param { Object } options 配置项:color-等高线颜色;width-等高线宽度
 * @returns { Array<Cesium.GroundPolylinePrimitive> } 贴地线Primitive图元
 */
ContourLine.prototype.createGroundPolylinePrimitive = function (
    appearance,
    options = {color: Cesium.Color.RED, width: 1.5},
) {
    appearance = Cesium.defaultValue(appearance, ContourLine.defaultAppearance);
    options = {
        ...defaultOptions,
        ...(options || {}),
    };
    const positionList = this.positionsList;
    const listSize = positionList.length;
    // const polylineCollection = new Array();
    const geometryInstances = [];
    for (let i = 0; i < listSize; i++) {
        const positions = positionList[i];
        const geometry = new Cesium.GroundPolylineGeometry({
            positions: positions, //tinItem.vertexes.map((vertex) => vertex.position),
            // loop: true,//无需闭合
            width: options.width,
        });
        const instance = new Cesium.GeometryInstance({
            geometry: geometry,
            attributes: {
                color: Cesium.ColorGeometryInstanceAttribute.fromColor(
                    // Cesium.Color.fromRandom()
                    Cesium.Color.RED.withAlpha(0.75),
                ),
            },
        });
        geometryInstances.push(instance);
    }
    return new Cesium.GroundPolylinePrimitive({
        geometryInstances: geometryInstances,
        appearance: appearance,
    });
};
export default ContourLine;
